﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class GrassBendingRenderFeature : ScriptableRendererFeature
{
    
    public class  CustomRendererPass : ScriptableRenderPass
    {
        public static readonly int GRASS_BENDING_SHADER_ID = Shader.PropertyToID("_GrassBendingRT");
        public static readonly RenderTargetIdentifier GRASS_BENDING_RTI = new RenderTargetIdentifier(GRASS_BENDING_SHADER_ID);
        ShaderTagId m_shaderTagId = new ShaderTagId("GrassBending");
        
        
        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            cmd.GetTemporaryRT(GRASS_BENDING_SHADER_ID,new RenderTextureDescriptor(512,512,RenderTextureFormat.R8, 0));
            ConfigureTarget(GRASS_BENDING_RTI);
            ConfigureClear(ClearFlag.All, Color.white);
            
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            var grassInstance = Grass.instnace;
            if (!grassInstance) return;
            CommandBuffer cmd = CommandBufferPool.Get("GrassBendingRT");
            
            Matrix4x4 viewMatrix = Matrix4x4.TRS(grassInstance.transform.position  + new Vector3(0,1,0), Quaternion.LookRotation(-Vector3.up),new Vector3(1,1,-1)).inverse;

            float sizeX = grassInstance.transform.localScale.x;
            float sizeZ = grassInstance.transform.localScale.z;
            
            Matrix4x4 projectMatrix = Matrix4x4.Ortho(-sizeX, sizeX, - sizeZ, sizeZ,0.5f, 1.5f);
            
            
            cmd.SetViewProjectionMatrices(viewMatrix, projectMatrix);
            
            //cmd.SetViewProjectionMatrices(renderingData.cameraData.camera.worldToCameraMatrix,renderingData.cameraData.camera.projectionMatrix);
            context.ExecuteCommandBuffer(cmd);
            var drawSetting = CreateDrawingSettings(m_shaderTagId, ref renderingData, SortingCriteria.CommonTransparent);
            
            var filterSetting = new FilteringSettings(RenderQueueRange.all);
            
            context.DrawRenderers(renderingData.cullResults, ref drawSetting, ref filterSetting);
            
            cmd.Clear();
            cmd.SetViewProjectionMatrices(renderingData.cameraData.camera.worldToCameraMatrix,renderingData.cameraData.camera.projectionMatrix);
            
            cmd.SetGlobalTexture(GRASS_BENDING_SHADER_ID, new RenderTargetIdentifier(GRASS_BENDING_SHADER_ID));
            
            context.ExecuteCommandBuffer(cmd);
            
            CommandBufferPool.Release(cmd);

        }

        public override void FrameCleanup(CommandBuffer cmd)
        {
            cmd.ReleaseTemporaryRT(GRASS_BENDING_SHADER_ID);
        }
    }


    private CustomRendererPass m_renderPass = null;
    
    public override void Create()
    {
        m_renderPass = new CustomRendererPass();
        m_renderPass.renderPassEvent = RenderPassEvent.AfterRenderingPrePasses;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(m_renderPass);
    }
}